<template>
  <div class="login-container">
    <el-form class="login-form" ref="form" :model="form" :rules="formRules" label-position="left">
      <div class="title-container">
        <h3 class="title">系统登录</h3>
      </div>
      <el-form-item prop="account">
        <span class="svg-container">
          <i class="iconfont icon-icon_user"></i>
        </span>
        <el-input v-model="form.account" name="account" type="text" auto-complete="on"/>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <i class="iconfont icon-mima"></i>
        </span>
        <el-input :type="pageData.passwordType" v-model="form.password" name="password" auto-complete="on"/>
        <span class="show-pwd" @click="handleChangeEyeStatus">
          <i :class="pageData.eyeStatus"></i>
        </span>
      </el-form-item>
      <el-button type="primary" style="width:100%;margin-bottom:30px;" :loading="pageData.loading" @click.native.prevent="handleUserLogin('form')">系统登录</el-button>
    </el-form>
  </div>
</template>

<script>
// 组件引入
// 方法工具引入
import { getRole } from "@/utils/UserCookies";
// 接口引入
export default {
  name: "login",
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data (){
    return {
      pageData:{
        passwordType: "password",// 密码表单展示类型
        eyeStatus: "iconfont icon-eye-close",// 密码表单展示图标
        loading: false,// 登录LOAD图
      },// 页面配置数据
      form:{
        account: "",// 账号
        password: ""// 密码
      },// 页面表单
      formRules: {
        account: [{validator: (rule, value, callback) => {
          if (value === ''){
            this.$message.warning("请输入登录账号");
            callback(new Error('请输入登录账号'));
          } else {
            callback();
          }
        }, trigger: "blur"}],
        password: [{validator: (rule, value, callback) => {
            if (value === ''){
              this.$message.warning("请输入登录密码");
              callback(new Error('请输入登录密码'));
            } else {
              callback();
            }
        }, trigger: "blur"}]
      },// 表单验证
      responseData: {},// 接口响应数据
    }
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 事件函数
    handleChangeEyeStatus(){
      if (this.pageData.passwordType === "password"){
        this.pageData.passwordType = "text";
        this.pageData.eyeStatus = "iconfont icon-eye-open";
      } else {
        this.pageData.passwordType = "password";
        this.pageData.eyeStatus = "iconfont icon-eye-close";
      }
    },// 显示隐藏密码
    handleUserLogin(formName){
      this.pageData.loading = true;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.cmsUserLogin();
        } else {
          this.pageData.loading = false;
          return false
        }
      })
    },// 登录按钮点击事件--进行表单字段验证
    // 接口
    cmsUserLogin(){
      let _this = this;
      let loginParams = {
        userAccount: _this.form.account,
        userPassword: _this.form.password
      };
      this.$store.dispatch("cmsUserLogin", loginParams).then(response => {
        if (response.code === 200){
          _this.$message.success("欢迎使用本系统");
          setTimeout(function () {
            let UserRole = getRole();
            if (UserRole === "95"){
              _this.$jump.push("/QAExpertManage");
            } else {
              _this.$jump.push("/home");
            }
          }, 800)
        } else {
          _this.pageData.loading = false;
          _this.$message.error(response.message)
        }
      });
    }// 登录接口调用
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created: function(){
    let _this = this;
    document.onkeydown = function(e) {
      if (!_this.pageData.loading) {
        let key = window.event.keyCode;
        if (key === 13) {
          _this.handleUserLogin('form'); //此方法是当按下enter键后要做的动作。
        }
      }
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style rel="stylesheet/scss" lang="scss">
  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  $bg:#283443;
  $light_gray:#eee;
  $cursor: #fff;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .login-container .el-input input{
      color: $cursor;
      &::first-line {
        color: $light_gray;
      }
    }
  }

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .login-container {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    .login-form {
      position: relative;
      width: 420px;
      max-width: 100%;
      padding: 160px 35px 0;
      margin: 0 auto;
      overflow: hidden;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }
    .title-container {
      position: relative;
      .title {
        font-size: 26px;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }
      .set-language {
        color: #fff;
        position: absolute;
        top: 5px;
        right: 0px;
      }
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .thirdparty-button {
      position: absolute;
      right: 0;
      bottom: 6px;
    }
  }
</style>
